package com.example.cpsc.sc.Swagger.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.PostMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@EnableSwagger2  //开启swagger
@Configuration
public class SwaggerConfig {


    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }



    //配置了swagger的Docke的bean实例
    //enable是否启动swagger，如果为false，则swagger不能再浏览器中
    @Bean
    public Docket docket(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        //获取项目的的环境
        //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean b = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .enable(b)
                //配置多个组
                .groupName("组名")
                //如果b为false会出现Could not render n, see the console.
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式 basePackage("com.example.cpsc.sc.Controller")
                //.basePackage()指定要扫描的包
                //any()扫描全部
                //none()都不扫描
                //withMethodAnnotation(RestController.class)扫描方法上的注解,扫描类上有RestController的类
                //withClassAnnotation()扫描类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.example.cpsc.sc.Controller"))
                //过滤路径
                //.paths(PathSelectors.ant("/com.example.cpsc.sc/**"))
                .build();//工厂模式
    }

    //配置ApiInfo信息
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("崔鹏","https://blog.csdn.net/qq_45945235?type=blog","1138857776@qq.com");
        return new ApiInfo(
                "Api文档",
                "描述",
                "版本1.0",
                "https://blog.csdn.net/qq_45945235?type=blog",
                 contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    }
}
